﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
using System.Net;

public partial class MembershipCard_Ticket_quandetail : WeiPage
{
    TicketBO tbo = new TicketBO();
    PersonInfoBO fbo = new PersonInfoBO();
    TicketForPersonBO pbo = new TicketForPersonBO();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["code"] != null)
            {
                try
                {
                    string url = string.Format(@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + GetAppid() + "&secret=" + GetSecret() + "&code={0}&grant_type=authorization_code", Request.QueryString["code"].ToString());
                    string token = PostRequest(url);
                    if (token.Contains("7200"))
                    {
                        string[] b = token.Split('\"');
                        Session["FromUserName"] = b[13];
                        Session["ToUserName"] = GetAppid();
                        // Response.Write(b[13]);
                    }
                }
                catch (Exception)
                {

                }
            }

            if (Request.QueryString["ID"] != null)
            {
                string[] id = Request.QueryString["ID"].ToString().Split(',');
                // AlertMessage(id[1].ToString());
                Ticket t = tbo.GetModel(int.Parse(id[0]));
                nr.InnerHtml = string.Format(@"<li class='{1}'>
         <p>{0}</p>
         <p><strong>{2}</strong>折 </p>
         <p>&nbsp;</p>
            
        <article>
        {4}
        </article>
        <article>已领取<strong>{3}</strong>份！</article>
       </li> ", t.Name, t.Type == "抵扣券" ? "diyong" : t.Type == "现金券" ? "xianjin" : "tejiaquan", t.Discount, t.HavNum, t.Describe.Replace("\n\t", "<br>").Replace("\n", "<br>"));
                Image2.ImageUrl = t.Pic;

                if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
                {
                    string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
                    string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
                    Session["FromUserName"] = user;
                    Session["ToUserName"] = user2;
                    PersonInfo per = fbo.GetModel(user);
                    if (per != null)
                    {
                        TicketForPerson p = pbo.GetModel(t.ID, user);
                        if (p != null)
                        {
                            div1.Visible = true;
                            div2.Visible = false;
                            if (id[1] == "0") //已领取的从列表页面进入不显示按钮
                            {
                                div1.Visible = false;
                                div2.Visible = true;
                            }
                        }
                    }
                }
            }
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
        {
            string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
            string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
            Session["FromUserName"] = user;
            Session["ToUserName"] = user2;
            PersonInfo per = fbo.GetModel(user);
            string url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + Token() + "&openid=" + user;
            string b = PostRequest(url);
            string[] a = b.Split('\"');
            if (a.Length != 35)
            {
                Response.Redirect("http://mp.weixin.qq.com/s?__biz=MjM5MjA3OTkxNw==&mid=200298770&idx=1&sn=028f10006dea006b4f70f9cace0cbbfc#rd");
                return;
            }
            if (per != null)
            {
                if (Request.QueryString["id"] != null)
                {
                    string[] ids = Request.QueryString["id"].ToString().Split(',');
                    if (ids.Length > 1)
                    {
                        Ticket t = tbo.GetModel(int.Parse(ids[0]));
                        if (t != null)
                        {
                            TicketForPerson p1 = pbo.GetModel(t.ID, user);
                            if (p1 != null)
                            {
                                AlertMessage("你已领取此优惠券，不可重复领取。");
                                return;
                            }
                            TicketForPerson p = new TicketForPerson();
                            p.FromUserName = user;
                            p.GetTime = DateTime.Now;
                            p.Store = "";
                            p.UseTime = DateTime.Parse("1900-01-01");
                            p.TicketID = t.ID;
                            int id = pbo.Add(p);
                            if (id > 0)
                            {
                                t.UsableNum--;
                                t.HavNum++;
                                tbo.Update(t);
                                Response.Redirect("myquan.aspx");
                            }
                        }
                    }
                }

            }
            else
            {
                AlertRedirect("请先注册绑定", "../../Spa/weixinreg2.aspx");
                Response.Redirect("../../Spa/weixinreg2.aspx");
            }
        }
        else
        {
            AlertMessage("请先关注汉方SPA官方微信");
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["ID"] != null)
        {
            string[] ids = Request.QueryString["id"].ToString().Split(',');
            if (ids.Length > 1)
            {
                Ticket t = tbo.GetModel(int.Parse(ids[0]));
                if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
                {
                    string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
                    string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
                    Session["FromUserName"] = user;
                    Session["ToUserName"] = user2;
                    PersonInfo per = fbo.GetModel(user);
                    // ServicePointManager.ServerCertificateValidationCallback = ValidateServerCertificate;
                    string url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + Token() + "&openid=" + user;
                    string b = PostRequest(url);
                    string[] a = b.Split('\"');
                    if (a.Length != 35)
                    {
                        Response.Redirect("http://mp.weixin.qq.com/s?__biz=MjM5MjA3OTkxNw==&mid=200298770&idx=1&sn=028f10006dea006b4f70f9cace0cbbfc#rd");
                        return;
                    }
                    if (per != null)
                    {
                        TicketForPerson p = pbo.GetModel(t.ID, user);
                        if (p != null)
                        {
                            Response.Redirect("pass.aspx?id=" + p.ID);
                            //p.UseTime = DateTime.Now;
                            //pbo.Update(p);
                            //Response.Redirect("myquan.aspx");
                        }
                    }
                }
            }
        }
    }
}